package com.zho.common.social;

import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;
import twitter4j.conf.ConfigurationBuilder;

import com.zho.common.utility.Constants;
import com.zho.domain.model.SnsPostingInfo;
import com.zho.util.Constants.Social;
import com.zho.util.Helpers;
import com.zho.util.Logit;

/**
 * @ClassName: TwitterPlugin
 * @Version 1.0
 * @author chu.anh.tuan
 * @CrDate February 09, 2012
 * 
 */
public class TwitterPlugin {

	private static Logit log = Logit.getInstance(TwitterPlugin.class);

	public static String CONSUMER_KEY = "";
	public static String CONSUMER_SECRET = "";

	static {
		try {
			CONSUMER_KEY = Helpers.getCgsProperty(Social.TWITTER_CONSUMER_KEY);
			CONSUMER_SECRET = Helpers
					.getCgsProperty(Social.TWITTER_CONSUMER_SECRET);
		} catch (Exception e) {
			log.error("TwitterPlugin load config fail", e);
		}
	}

	public static Twitter getTwitterInstance(ConfigurationBuilder cb) {
		try {
			if (cb != null) {
				return new TwitterFactory(cb.build()).getInstance();
			} else {
				return new TwitterFactory().getInstance();
			}
		} catch (Exception e) {
			log.error("TwitterPlugin.getTwitterInstance(): error", e);
			return null;
		}
	}

	public static RequestToken getOAuthRequestToken(Twitter tw4j) {
		if (tw4j == null) {
			return null;
		}
		try {
			tw4j.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
			return tw4j.getOAuthRequestToken();
		} catch (TwitterException e) {
			log.error("TwitterPlugin.getRequestToken(Twitter): error", e);
			return null;
		}
	}

	public static String getAuthorizationURL(RequestToken requestToken) {
		try {
			return requestToken.getAuthorizationURL();
		} catch (Exception e) {
			log.error("TwitterPlugin.getAuthorizationURL(RequestToken): error",
					e);
			return null;
		}
	}

	public static AccessToken getAccessToken(Twitter tw4j,
			RequestToken requestToken, String oauth_verifier) {

		try {
			return tw4j.getOAuthAccessToken(requestToken, oauth_verifier);
		} catch (Exception e) {
			log.error(
					"TwitterPlugin.getAccessToken(Twitter, RequestToken, String): error",
					e);
			return null;
		}
	}

	public static int postMessage(String oAuthAccessToken,
			String oAuthAccessTokenSecret, String message) {
		ConfigurationBuilder cb = new ConfigurationBuilder();
		cb.setDebugEnabled(true).setOAuthConsumerKey(CONSUMER_KEY)
				.setOAuthConsumerSecret(CONSUMER_SECRET)
				.setOAuthAccessToken(oAuthAccessToken)
				.setOAuthAccessTokenSecret(oAuthAccessTokenSecret);
		TwitterFactory tw4jFactory = new TwitterFactory(cb.build());
		Twitter tw4j = tw4jFactory.getInstance();
		try {
			Status status = tw4j.updateStatus(message);
			if (status.getId() > 0) {
				return 1;
			} else {
				return 2;
			}
		} catch (TwitterException e) {
			log.error(
					"TwitterPlugin.postMessage(String, String, String): error",
					e);
			return 3;
		}
	}

	public static int postMessage() {
		/*
			String oAuthAccessTokenSecret, SnsPostingInfo snsPostingInfo) {
		ConfigurationBuilder cb = new ConfigurationBuilder();
		cb.setDebugEnabled(true).setOAuthConsumerKey(CONSUMER_KEY)
				.setOAuthConsumerSecret(CONSUMER_SECRET)
				.setOAuthAccessToken(oAuthAccessToken)
				.setOAuthAccessTokenSecret(oAuthAccessTokenSecret);
		TwitterFactory tw4jFactory = new TwitterFactory(cb.build());
		Twitter tw4j = tw4jFactory.getInstance();
		try {
			String message = "";
			if (snsPostingInfo.getPropertyKey().equals(
					Constants.SnSPostingType.SHARE_BET)
					|| snsPostingInfo.getPropertyKey().equals(
							Constants.SnSPostingType.CREATE_BET)) {
				String url = Helpers
						.getCgsProperty(com.zho.util.Constants.Address.FRONT_URL)
						+ "/bet/viewBet?betInfo.betId="
						+ snsPostingInfo.getPropertyValue();
				message = "Shares the Bet " + url;
			} else if (snsPostingInfo.getPropertyKey().equals(
					Constants.SnSPostingType.SHARE_EVENT)
					|| snsPostingInfo.getPropertyKey().equals(
							Constants.SnSPostingType.CREATE_EVENT)) {
				String url = Helpers
						.getCgsProperty(com.zho.util.Constants.Address.FRONT_URL)
						+ "/event/events?model.eventCondition.eventId="
						+ snsPostingInfo.getPropertyValue();
				message = url;
			} else if (snsPostingInfo.getPropertyKey().equals(
					Constants.SnSPostingType.SHARE_TOURNEY)
					|| snsPostingInfo.getPropertyKey().equals(
							Constants.SnSPostingType.CREATE_TOURNEY)) {
				String url = Helpers
						.getCgsProperty(com.zho.util.Constants.Address.FRONT_URL)
						+ "/tourney/viewTourney?tourneyInfo.tourneyId="
						+ snsPostingInfo.getPropertyValue();
				message = url;
			} else if (snsPostingInfo.getPropertyKey().equals(
					Constants.SnSPostingType.COMMENT_BET)) {

			} else if (snsPostingInfo.getPropertyKey().equals(
					Constants.SnSPostingType.COMMENT_EVENT)) {

			} else if (snsPostingInfo.getPropertyKey().equals(
					Constants.SnSPostingType.COMMENT_TOURNEY)) {

			} else if (snsPostingInfo.getPropertyKey().equals(
					Constants.SnSPostingType.JOIN_BET)) {
				String url = Helpers
						.getCgsProperty(com.zho.util.Constants.Address.FRONT_URL)
						+ "/bet/viewBet?betInfo.betId="
						+ snsPostingInfo.getPropertyValue()
						+ "&custBetPosInfo.custBetPosId="
						+ snsPostingInfo.getDescription();
				message = "Joined the Bet " + url;
			} else if (snsPostingInfo.getPropertyKey().equals(
					Constants.SnSPostingType.NOTICE)) {

			}
			if (!Helpers.isEmpty(message)) {
				Status status = tw4j.updateStatus(message);
				if (status.getId() > 0) {
					return SnSPostingStatus.SUCCESS;
				}
			}
			return SnSPostingStatus.FAIL;
		} catch (Exception e) {
			TwitterException ee = (TwitterException) e;
			System.out.println(ee.getStatusCode());// b2b52c28-1b442886
			log.error(
					"TwitterPlugin.postMessage(String, String, SnsPostingInfo): error",
					e);
			return SnSPostingStatus.FAIL;
		}
		*/
		return 0;
	}

}
